<script>
import VueMarkdown from 'vue-markdown'

export default {
  name: 'MarkdownPreview',
  components: {
    VueMarkdown,
  },
  data() {
    return {
      fileContent: '',
    }
  },
  beforeMount() {
    const fileUrl = this.$route.query.url
      ? decodeURIComponent(this.$route.query.url)
      : 'manual/2025重庆市城市体检信息平台操作指南.md'
    if (fileUrl) {
      fetch(fileUrl)
        .then((res) => {
          if (res.status === 200) {
            return res.text()
          } else {
            return ''
          }
        })
        .then((res) => {
          this.fileContent = res
        })
    }
  },
  // watch: {
  //   '$route.query.url'(val) {
  //     if (val) {
  //       const fileUrl = decodeURIComponent(val)
  //       fetch(fileUrl)
  //         .then((res) => {
  //           if (res.status === 200) {
  //             return res.text()
  //           } else {
  //             return ''
  //           }
  //         })
  //         .then((res) => {
  //           this.fileContent = res
  //         })
  //     } else {
  //       this.fileContent = ''
  //     }
  //   },
  // },
}
</script>

<template>
  <div class="markdown-preview">
    <!-- 目录 -->
    <div v-if="fileContent" class="toc-container" id="toc-container-id"></div>

    <!-- 内容 -->
    <VueMarkdown
      v-if="fileContent"
      class="content-container"
      :source="fileContent"
      :toc="true"
      :toc-first-level="1"
      :toc-last-level="6"
      toc-id="toc-container-id"
    />
  </div>
</template>

<style lang="scss" scoped>
.markdown-preview {
  width: 100%;
  height: 100%;
  display: flex;
  overflow: hidden;

  :deep(.toc-container) {
    width: 330px;
    flex-shrink: 0;
    border-right: 1px solid #eaecef;
    padding: 20px;
    overflow-y: auto;

    ul {
      padding-inline-start: 0;
      margin-block-start: 0;
      margin-block-end: 0;
      list-style: none;

      li + li {
        margin-top: 10px;
      }

      li > ul {
        margin-top: 10px;
        padding-left: 10px;
      }
    }

    a {
      color: #333;
      text-decoration: none;
      font-size: 16px;
      cursor: pointer !important;

      &:hover {
        color: #009879;
      }
    }
  }

  :deep(.content-container) {
    padding: 20px;
    flex-grow: 1;
    overflow-y: auto;

    table.table {
      width: 100%;
      border-collapse: collapse;
      margin: 20px 0;
      font-size: 1em;
      font-family: sans-serif;
      min-width: 400px;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);

      thead tr {
        background-color: #009879;
        color: #ffffff;
        text-align: left;
      }

      th,
      td {
        padding: 12px 15px;
        border: 1px solid #dddddd;
      }

      tbody tr {
        border-bottom: 1px solid #dddddd;
      }

      tbody tr:nth-of-type(even) {
        background-color: #f3f3f3;
      }

      tbody tr:last-of-type {
        border-bottom: 2px solid #009879;
      }

      tbody tr:hover {
        background-color: #f1f1f1;
        cursor: pointer;
      }
    }

    pre {
      background-color: #f5f5f5;
      border: 1px solid #eee;
      border-radius: 4px;
      padding: 12px 16px;
      overflow: auto;
    }

    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      a.toc-anchor {
        display: none;
      }
    }

    h2 {
      padding-left: 20px;
    }

    h3 {
      padding-left: 40px;
    }

    h4 {
      padding-left: 60px;
    }

    h5 {
      padding-left: 80px;
    }

    h6 {
      padding-left: 100px;
    }
  }
}
</style>
